package com.zlt.bkparking.service.impl;


import com.zlt.bkparking.dto.CarAddressDTO;
import com.zlt.bkparking.entity.CarAddress;
import com.zlt.bkparking.entity.CarAddressExample;
import com.zlt.bkparking.entity.ParkingLot;
import com.zlt.bkparking.entity.ParkingLotExample;
import com.zlt.bkparking.mapper.CarAddressMapper;
import com.zlt.bkparking.mapper.ParkingLotMapper;
import com.zlt.bkparking.service.CarAddressService;
import com.zlt.bkparking.vo.ParkingLotVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 停车场车位管理类
 */
@Service
public class CarAddressServiceImpl implements CarAddressService {

    @Autowired
    private CarAddressMapper carAddressMapper;

    @Autowired
    private ParkingLotMapper parkingLotMapper;

    /**
     * 停车场车位相关操作
     * @param carAddressDTO
     * @return
     */
    @Override
    public boolean updateCarAddress(CarAddressDTO carAddressDTO) {

        if(carAddressDTO==null||carAddressDTO.getPlId()==null){
            throw new RuntimeException("车位carAddressDTO，传参为空");
        }

        ////////////////////等待ActiveUser//////////////////////////////
/*        ActiveUser activeUser = SecurityUtils.getSubject().getPrincipal();
        if(activeUser==null){
            throw new RuntimeException("当前未登录");
        }*/

        CarAddressExample carAddressExample=new CarAddressExample();
        carAddressExample.createCriteria().andPlIdEqualTo(carAddressDTO.getPlId());
        List<CarAddress> carAddresses = carAddressMapper.selectByExample(carAddressExample);
        CarAddress carAddress=new CarAddress();
        BeanUtils.copyProperties(carAddressDTO,carAddress);
        if(carAddresses==null||carAddresses.isEmpty()){
            return carAddressMapper.insertSelective(carAddress)>0;
        }else {
            carAddress.setCaId(carAddresses.get(0).getCaId());
            return carAddressMapper.updateByPrimaryKeySelective(carAddress)>0;
        }
    }


    /**
     * 查询停车场 by 用户ID
     * @return
     */
    public List<ParkingLotVO> selectParkingLotBybuId(){
        ////////////////////等待ActiveUser//////////////////////////////
/*        ActiveUser activeUser = SecurityUtils.getSubject().getPrincipal();
        if(activeUser==null){
            throw new RuntimeException("当前未登录");
        }*/

        ParkingLotExample parkingLotExample=new ParkingLotExample();
//test        parkingLotExample.createCriteria().andBuIdEqualTo(activeUser.getBuId);
        parkingLotExample.createCriteria().andBuIdEqualTo(1l);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if(parkingLots==null || parkingLots.isEmpty()){
            return null;
        }
        List<ParkingLotVO> parkingLotVOS=new ArrayList<>();
        for (ParkingLot parkingLot : parkingLots) {
            ParkingLotVO parkingLotVO=new ParkingLotVO();
            BeanUtils.copyProperties(parkingLot,parkingLotVO);
            parkingLotVOS.add(parkingLotVO);
        }
        return parkingLotVOS;
    }
}
